import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import AsyncStorage from '@react-native-async-storage/async-storage';
import LoginScreen from './login';
import TabNavApp from '../TabNavApp';
import RegisterScreen from './register';
import ChangePasswordScreen from './nextPassword';
import FindPasswordScreen from './findpassword';
const Stack = createStackNavigator();

export default function App() {
  const [initialRouteName, setInitialRouteName] = React.useState(null);

  React.useEffect(() => {
    const fetchToken = async () => {
      const loginStatus = await AsyncStorage.getItem('loginStatus');
      console.log(loginStatus);
      if (loginStatus==="true") {
        setInitialRouteName('TabNavApp');
      } else {
        setInitialRouteName('Login');
      }
    };

    fetchToken();
  }, []);

  if (initialRouteName === null) {
    return null;
  }

  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName={initialRouteName}>
        <Stack.Screen name="Login" component={LoginScreen} options={{ headerShown: false }}/>
        <Stack.Screen name="TabNavApp" component={TabNavApp} options={{ headerShown: false }}/>
        <Stack.Screen name="Register" component={RegisterScreen} />
        <Stack.Screen name="FindPassword" component={FindPasswordScreen} />
        <Stack.Screen name="NextPassword" component={ChangePasswordScreen} />
      </Stack.Navigator>
    </NavigationContainer>
  );
}
